﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using JiveMessenger.Network;
using System.Windows.Media.Imaging;

namespace JiveMessenger.Models
{
    public class ContactModel : DependencyObject, INotifyPropertyChanged
    {
        public string UserName { get; set; }
        public string UserID { get; set; }

        private string _nickName;

        public string NickName
        {
            get { return _nickName; }
            set
            {
                if (value != this._nickName)
                {
                    this._nickName = value;
                    RaisePropertyChanged("NickName");
                }
            }
        }

        private int _unreadMessageCount;

        public int UnreadMessageCount
        {
            get { return _unreadMessageCount; }
            set
            {
                if (value != this._unreadMessageCount)
                {
                    this._unreadMessageCount = value;
                    RaisePropertyChanged("UnreadMessageCount");
                }
            }
        }

        private string _statusText;

        public string StatusText
        {
            get { return _statusText; }
            set
            {
                if (value != this._statusText)
                {
                    this._statusText = value;
                    RaisePropertyChanged("StatusText");
                }
            }
        }

        private string _pmStatus;

        public string PmStatus
        {
            get { return _pmStatus; }
            set
            {
                if (value != this._pmStatus)
                {
                    this._pmStatus = value;
                    RaisePropertyChanged("PmStatus");
                }
            }
        }


        public LinearGradientBrush ClientStatusBrush
        {
            get { return (LinearGradientBrush)GetValue(ClientStatusBrushProperty); }
             set { SetValue(ClientStatusBrushProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ClientStatusBrush.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ClientStatusBrushProperty =
            DependencyProperty.Register("ClientStatusBrush", typeof(LinearGradientBrush), typeof(ContactModel), new PropertyMetadata(null));

        public ImageSource AvatarImage
        {
            get { return (ImageSource)GetValue(AvatarImageProperty); }
            set { SetValue(AvatarImageProperty, value); }
        }

        // Using a DependencyProperty as the backing store for AvatarImage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty AvatarImageProperty =
            DependencyProperty.Register("AvatarImage", typeof(ImageSource), typeof(ContactModel), new PropertyMetadata(null));

        public ContactModel(string userID, string nickName)//TODO: add support for user avatars)
        {
            this.AvatarImage = new BitmapImage(new Uri("/Assets/avatar_empty_50.png", UriKind.Relative));
            this.UserID = userID;
            this.UserName = UserID.Remove(userID.IndexOf("#"));
            this.NickName = nickName;
            StatusText = "Available";
            ClientStatusBrush = (LinearGradientBrush)App.Current.Resources["ButtonGradientBrushOnline"];

        }
        public ContactModel() { }
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
